﻿using Knlght.CH.Domain.IRepository;
using Knlght.Domain;
using Knlght.Model.Sys;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Knlght.Service
{
    public class RoleInfoService : ServiceBase
    {
        public ActionResponse Add(RoleInfo model)
        {
            return doAction(() =>
            {
                var rp = getRepository<IRoleInfoRepository>();
                return rp.Add(model);
            });
        }
        public ActionResponse Update(RoleInfo model)
        {
            return doAction(() =>
            {
                var rp = getRepository<IRoleInfoRepository>();
                return rp.Update(model);
            });
        }
        public ActionResponse Update(QueryRequest<RoleInfo> query, Expression<Func<RoleInfo, RoleInfo>> key)
        {
            return doAction(() =>
            {
                var rp = getRepository<IRoleInfoRepository>();
                return rp.UpdateToList(query, key);
            });
        }
        public ActionResponse Remove(QueryRequest<RoleInfo> query)
        {
            return doAction(() =>
            {          
                var rp = getRepository<IRoleInfoRepository>();
                return rp.Delete(query);
            });
        }
      
        public ItemResponse<RoleInfo> GetModel(QueryRequest<RoleInfo> query)
        {
            return GetModel<RoleInfo>(() =>
            {
                var rp = getRepository<IRoleInfoRepository>();
                return rp.GetModel(query);
            });
        }
        public PagingResponse<RoleInfo> Paging(PagingRequest<RoleInfo> request)
        {
            return GetPaging<RoleInfo>(() => {
                //总记录
                int totalRecord = 0;
                var rp = getRepository<IRoleInfoRepository>();
                return new PagingList<RoleInfo>()
                {
                    Models = rp.GetList(request.PageSize, request.PageIndex, out totalRecord, request.Query),
                    TotalModels = totalRecord,
                    TotalPages = (totalRecord + request.PageSize - 1) / request.PageSize
                };
            });
        }
        public ListResponse<RoleInfo> GetList()
        {
            return GetList<RoleInfo>(() =>
            {
                var rp = getRepository<IRoleInfoRepository>();
                return rp.GetList().ToList();
            });
        }
        public ListResponse<RoleInfo> GetList(QueryRequest<RoleInfo> query)
        {
            return GetList(() =>
            {
                var rp = getRepository<IRoleInfoRepository>();
                return rp.GetList(query);
            });
        }
    }
}
